#include "commonpage.h"
#include "ui_commonpage.h"

#include"listitembox.h"

#include<QDebug>

CommonPage::CommonPage(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::CommonPage)
{
    ui->setupUi(this);
    //将QListWidget中的水平滑动条去掉
    ui->pageMusicList->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

//    QString style = "QScrollBar::handle:vertical{width:10px;background-color:#EFEFEF;border-radius:5px; min-height:20px;}";
//    ui->pageMusicList->setStyleSheet(style);
}

CommonPage::~CommonPage()
{
    delete ui;
}

void CommonPage::setPageType(PageType pageType)
{
    this->pageType = pageType;
}

void CommonPage::setCommonPageUi(const QString &title, const QString &imagePath)
{
    // 设置标题
    ui->pageTitle->setText(title);
    // 设置封⾯栏图片
    ui->musicImageLabel->setPixmap(QPixmap(imagePath));
    //将封面栏图片尺寸自适应QLabel大小
    ui->musicImageLabel->setScaledContents(true);

    //当播放全部的按钮被按下时信号和槽函数
    connect(ui->playAllBt,&QPushButton::clicked,this,[=](){

        //发送给qqmusic当前pageType标记的页面中的所有歌曲
        emit playAll(pageType);
    });

    //双击播放歌曲的信号关联槽(索引播放歌曲)
    connect(ui->pageMusicList,&QListWidget::doubleClicked,this,[=](const QModelIndex &index){
        //鼠标双击后发送信号告诉qqmusic播放被双击的歌曲
        emit playMusicByIndex(this,index.row());
    });

}

void CommonPage::addMusicToMusicPage(MusicList &musicList)
{
    musicOfPage.clear();
    for(auto e : musicList)
    {
        switch (pageType)
        {
        case PageType::LIKE_PAGE:
            if(e.getIsLike())
            {
                musicOfPage.push_back(e.getMusicId());
            }
            break;

        case PageType::LOCAL_PAGE:
            musicOfPage.push_back(e.getMusicId());
            break;

        case PageType::HISTORY_PAGE:
            if(e.getIsHistory())
            {
                musicOfPage.push_back(e.getMusicId());
            }
            break;

        default:
            qDebug()<<"暂不支持";
        }
    }
}

void CommonPage::refresh( MusicList& musicList)
{
    //删除之前的歌曲
    ui->pageMusicList->clear();
    //先将musicId添加到musicOfPage
    addMusicToMusicPage(musicList);

    for(auto musicId : musicOfPage)
    {
        //要添加当前的音乐就先在musiList中检查其是否存在
       auto it = musicList.findMusicById(musicId);

       //存在
       ListItemBox* listItemBox = new ListItemBox(this);
       listItemBox->setSingerName(it->getSingerName());
       listItemBox->setMusicName(it->getMusicName());
       listItemBox->setAlbum(it->getAlbumName());
       listItemBox->setLikeMusic(it->getIsLike());


       QListWidgetItem* listWidgetItem = new QListWidgetItem(ui->pageMusicList);
       listWidgetItem->setSizeHint(QSize(ui->pageMusicList->width(), 45));
       //QListWidget中添加自定义的控件，使用前要先把自定义控件绑定到QListWidgetItem中
       ui->pageMusicList->setItemWidget(listWidgetItem, listItemBox);

       connect(listItemBox,&ListItemBox::setIsLike,this,[=](bool isLike){
           //将music中的isLike修改
           //it->setIsLike(isLike);
           //发送信号让qqMusic中likepage、rencentpage localpage更新歌曲状态
           emit updateLikeMusic(isLike,it->getMusicId());
       });
    }



    //触发窗口重绘painEvent
    //update和repaint方法的区别
    //update只是把painEvent事件放到窗口事件的循环队列中，并没有立即执行
    //而调用rapaint会立即重绘该事件
    //update();
    repaint();

}

void CommonPage::addMusicToPlayer(MusicList &musicList, QMediaPlaylist *playList)
{
    for(auto e: musicList)
    {
        switch (pageType)
        {
        case LIKE_PAGE:
        {
            if(e.getIsLike())
            {
                playList->addMedia(e.getMuicUrl());
            }
            break;
        }
        case LOCAL_PAGE:
        {
            playList->addMedia(e.getMuicUrl());
            break;
        }
        case HISTORY_PAGE:
        {
            if(e.getIsHistory())
            {
                playList->addMedia(e.getMuicUrl());
            }
            break;
        }
        default:
         break;
        }

    }
}

QString CommonPage::getMusicIdByIndex(int index)
{
    if(index >= musicOfPage.size())
    {
        qDebug()<<"无此歌曲";
        return "";
    }
    return musicOfPage[index];
}

void CommonPage::setImageLabel(QPixmap pixMap)
{
    ui->musicImageLabel->setPixmap(pixMap);
    ui->musicImageLabel->setScaledContents(true);
}


































